<?php
class exercisemodity extends Model {
	/**
	 * 根据id 更新修改题目或者答案
	 *
	 * @param unknown_type $userID        	
	 * @param unknown_type $questionID        	
	 * @param unknown_type $intro        	
	 * @param unknown_type $type
	 *        	0-修改题目，1-修改答案
	 * @return bool resource
	 */
	public function insertTitleOrAndwer($userID, $questionID, $intro, $type) {
		$sql = "INSERT INTO `exercise_modify`
				(`ex_id`, `modify_create_userid`, `modify_create_time`, `modify_state`, `modify_type`, `modify_text`)
				VALUES
				( " . $questionID . ", " . $userID . ", NOW(), 0, " . $type . ",'" . $intro . "')";
		return $this->insert ( $sql );
	}
	
	/**
	 * 
	 * @param unknown_type $state
	 * @param unknown_type $page
	 * @param unknown_type $num
	 * @return multitype:unknown number Ambigous <boolean, multitype:>
	 */
	public function getExerciseModifyPageModel($state = 3, $subId = 0, $typeId = 0, $keywords = 0, $page = 1, $num = 10){
		$select = "SELECT `exercise_modify`.*, `exercise`.`ex_title`, `uuser`.`user_realname` FROM `exercise_modify` "
				 . "LEFT JOIN `exercise` ON `exercise_modify`.`ex_id` = `exercise`.`ex_id` "
				 . "LEFT JOIN `uuser` ON `exercise_modify`.`modify_create_userid` = `uuser`.`user_id` ";
		$filter = "WHERE 1 ";
		if ($state != 3){
			$filter .= "AND `modify_state` = '".$state."' ";
		}
		if ($subId){
			$filter .= "AND `exercise`.`sub_id` = '".$subId."' ";
		}
		if ($typeId){
			$filter .= "AND `exercise`.`et_id` = '".$typeId."' ";
		}
		if ($keywords){
			//echo $keywords;
			$filter .= "AND `exercise`.`ex_title` LIKE '%".$keywords."%' ";
		}
		$order = "ORDER BY `exercise_modify`.`modify_create_time` DESC ";
		$limit = "LIMIT ".($page-1)*$num.",".$num." ";
		$sql = $select.$filter.$order.$limit;
		$sqlTotal = $select.$filter.$order;
		//echo $sqlTotal;
		$list = $this->fetchAll($sql);
		$total = count($this->fetchAll($sqlTotal));
		$totalPage = ceil($total / $num);
		//echo $sql;
		return array('page'=>$page,'list'=>$list,'total'=>$total,'totalPage'=>$totalPage);
	}
	
	/**
	 * 根据Id获取申请修改习题的详情
	 * @param unknown_type $emId
	 * @return Ambigous <boolean, multitype:>
	 */
	public function getExerciseModifyDetaiById($emId){
		$sql = "SELECT `exercise_modify`.*, `exercise`.`ex_title`, `exercise`.`ex_answer`,`exercise`.`ex_pre_title`, `exercise`.`ex_pre_answer`, `uuser`.`user_realname`,`uuser`.`user_phone`, "
			 . "`subject`.`sub_cont`, `exercise_type`.`et_cont` "
			 . "FROM `exercise_modify` "
			 . "LEFT JOIN `exercise` ON `exercise_modify`.`ex_id` = `exercise`.`ex_id` "
			 . "LEFT JOIN `uuser` ON `exercise_modify`.`modify_create_userid` = `uuser`.`user_id` "
			 . "LEFT JOIN `subject` ON `exercise`.`sub_id` = `subject`.`sub_id` "
			 . "LEFT JOIN `exercise_type` ON `exercise`.`et_id` = `exercise_type`.`et_id` "
			 . "WHERE `exercise_modify`.`modify_id` = '".$emId."'";
		return $this->fetchRow($sql);
	}
	
	/**
	 * 改变申请修改状态
	 * @param unknown_type $staus
	 * @param unknown_type $emId
	 * @return resource
	 */
	public function changeStaus($staus,$emId){
		$sql = "UPDATE `howdo`.`exercise_modify` SET `modify_state` = '".$staus."' WHERE `exercise_modify`.`modify_id` = '".$emId."';";
		return $this->update($sql);
	}
	
	/**
	 * 删除申请修改记录
	 * @param unknown_type $emId
	 * @return resource
	 */
	public function delete($emId){
		$sql = "DELETE FROM `howdo`.`exercise_modify` WHERE `exercise_modify`.`modify_id` = '".$emId."'";
		return $this->del($sql);
	}
}